from django.db import models
from django.contrib.auth.models import User

# Create your models here.


# 项目表
class Project(models.Model):
    name = models.CharField(max_length=64, verbose_name='项目名称')
    desc = models.CharField(max_length=256, verbose_name='项目描述')
    status_choices = (
        (0, '开发中'),
        (1, '已上线'),
        (2, '已关闭'),
    )
    status = models.IntegerField(verbose_name='项目状态', default=0, choices=status_choices)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '项目'
        verbose_name_plural = verbose_name


# 项目成员表
class ProjectMember(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    role_choices = (
        (0, '开发'),
        (1, '测试'),
        (2, '管理'),
    )
    role = models.IntegerField(verbose_name='角色', default=0, choices=role_choices)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return f'{self.project.name} - {self.user.username}'

    class Meta:
        verbose_name = '项目成员'
        verbose_name_plural = verbose_name
        unique_together = ['project', 'user']

# 项目代码库
class ProjectRepository(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    name = models.CharField(max_length=64, verbose_name='仓库名称')
    url = models.CharField(max_length=256, verbose_name='仓库地址')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return f'{self.project.name} - {self.name}'

    class Meta:
        verbose_name = '项目代码库'
        verbose_name_plural = verbose_name
        unique_together = ['project', 'name']

# 项目问题
class ProjectIssue(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    name = models.CharField(max_length=64, verbose_name='问题名称')
    desc = models.CharField(max_length=256, verbose_name='问题描述')
    status_choices = (
        (0, '待解决'),
        (1, '已解决'),
    )
    status = models.IntegerField(verbose_name='问题状态', default=0, choices=status_choices)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return f'{self.project.name} - {self.name}'

    class Meta:
        verbose_name = '项目问题'
        verbose_name_plural = verbose_name
        unique_together = ['project', 'name']

# 项目任务
class ProjectTask(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    name = models.CharField(max_length=64, verbose_name='任务名称')
    desc = models.CharField(max_length=256, verbose_name='任务描述')
    status_choices = (
        (0, '待处理'),
        (1, '处理中'),
        (2, '已完成'),
    )
    status = models.IntegerField(verbose_name='任务状态', default=0, choices=status_choices)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return f'{self.project.name} - {self.name}'

    class Meta:
        verbose_name = '项目任务'
        verbose_name_plural = verbose_name
        unique_together = ['project', 'name']

# 项目版本
class ProjectVersion(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目')
    name = models.CharField(max_length=64, verbose_name='版本名称')
    desc = models.CharField(max_length=256, verbose_name='版本描述')
    status_choices = (
        (0, '待发布'),
        (1, '已发布'),
    )
    status = models.IntegerField(verbose_name='版本状态', default=0, choices=status_choices)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return f'{self.project.name} - {self.name}'

    class Meta:
        verbose_name = '项目版本'
        verbose_name_plural = verbose_name
        unique_together = ['project', 'name']